package com.yaolong.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaolong.commons.exceptions.BusinessException;
import com.yaolong.domain.Camera;
import com.yaolong.dto.CameraDto;
import com.yaolong.mapper.CameraMapper;
import com.yaolong.service.ICameraService;
import com.yaolong.commons.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yaolong
 * @since 2021-04-20
 */
@Service
public class CameraServiceImpl extends BaseServiceImpl<CameraMapper, Camera> implements ICameraService {


    @Override
    public CameraDto getWebcamByPlace(String place, String serialNumber) {
        QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("place",place).eq("serial_number",serialNumber);
        Camera one = super.getOne(queryWrapper);
        if (BeanUtil.isEmpty(one)){
            throw new BusinessException("未找到——地点："+place+",摄像头序列号："+ serialNumber);
        }
        CameraDto cameraDto = new CameraDto();
        BeanUtil.copyProperties(one,cameraDto);
        return cameraDto;
    }

    @Override
    public List<CameraDto> getWebcamListByPlace(String place) {
        QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("place",place);
        List<Camera> list = super.getList(queryWrapper);
        if (BeanUtil.isEmpty(list)){
            throw new BusinessException("未找到——地点："+place);
        }
        return list.stream().map(a -> {
            CameraDto cameraDto = new CameraDto();
            BeanUtil.copyProperties(a, cameraDto);
            return cameraDto;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean create(Camera domain) {
        domain.setCreateTime(LocalDateTime.now());
        return super.create(domain);
    }

    @Override
    public IPage<?> page(int current, int size, Camera domain) {
        Page<Camera> page = new Page<>(current, size);
        QueryWrapper<Camera> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(BeanUtil.isEmpty(domain.getPlace()),"place",domain.getPlace());
        return super.page(page, queryWrapper);
    }
}
